/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package slicktest.GameStates;

import java.awt.Font;
import java.util.ArrayList;
import java.util.List;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.TrueTypeFont;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import slicktest.constants.StateConstants;

/**
 * TODO: need to use an actual font to generate the menu text,
 * rather than some weird bass-ackwards scheme.
 * @author enrightt
 */
public class MenuState extends BasicGameState {

    private int id = -1;

    private static final int FONT_SIZE = 30;
    private static final int MENU_OFFSET = 60;
    private static final int OPTION_START_X = 724;
    private static final int OPTION_START_Y = 500;
    private Image background;
    private Image mainMenu;
    private List<String> options;
    private TrueTypeFont textFont;

    public MenuState(int id) {
        this.id = id;
        options = new ArrayList<String>();
        options.add("Play Game");
        options.add("Quit");
    }

    @Override
    public int getID() {
        return id;
    }

    public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
        background = new Image("data/menu_assets/menubackground.jpg");
        mainMenu = new Image("data/menu_assets/menutitle.png");
        this.textFont = new TrueTypeFont(new Font("ARIAL NARROW", Font.BOLD, FONT_SIZE), true);


    }

    public void render(GameContainer gc, StateBasedGame sbg, Graphics grphcs) throws SlickException {
        background.draw(0, 0);
        mainMenu.draw(0, 15);

        for (int i = 0; i < options.size(); i++) {
            textFont.drawString(OPTION_START_X, OPTION_START_Y + (i * MENU_OFFSET), options.get(i), Color.yellow);
        }
    }

    public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
        Input input = gc.getInput();
        float xPos = input.getMouseX();
        float yPos = input.getMouseY();

        boolean playGame = false;
        boolean quitGame = false;

        if (input.isMousePressed(Input.MOUSE_LEFT_BUTTON)) {
            int mouseX = input.getMouseX();
            int mouseY = input.getMouseY();

            if (mouseX > OPTION_START_X && mouseX < OPTION_START_X + textFont.getWidth("Play Game")) {
                for (int i = 0; i < options.size(); i++) {
                    String option = options.get(i);
                        
                    if (mouseY > OPTION_START_Y + (i * MENU_OFFSET) && mouseY < OPTION_START_Y + ((i+1) * MENU_OFFSET)) {
                        System.out.println("picked opt: " + option);
                        if(option.equals("Play Game")) {
                            playGame = true;
                        } else if(option.equals("Quit")) {
                            quitGame = true;
                        }
                    }
                }
            }
        }

        if (quitGame) {
            gc.exit();
        }

        if (playGame) {
            sbg.enterState(StateConstants.GAMEPLAY_STATE);
        }
    }
}
